{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "#load data from file导入txt数据\n",
    "def load_data(filename):\n",
    "    dataset = []\n",
    "    file = open(filename)\n",
    "    for line in file.readlines():\n",
    "        lineArr = line.strip().split('\\t')\n",
    "        m = len(lineArr)\n",
    "        dataset.append(lineArr[0:m])\n",
    "    return np.array(dataset,dtype=np.float64)\n",
    "           \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(80L, 2L)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEKCAYAAAASByJ7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGrNJREFUeJzt3W+sHFd5x/HfE0MVVKA3JS6pnLjmRd9EKXHrSxwrqkDl\nj0KJyAv6orRQUCv5DWT3miDKH8n3mhYhFcnxNUVCUVupKlSItiBSRAVBQukLgpXrYP4kARrRBEKD\nCCIWVFSC+D59MTu5s+P9M7s7M+ecme9HWvnOeu17dndmnnPO88wZc3cBAHBF6AYAAOJAQAAASCIg\nAABGCAgAAEkEBADACAEBACCJgAAAGCEgAAAkERAAACPPCd2ARVx99dV+6NCh0M0AgKScP3/+x+6+\nf97rkgoIhw4d0s7OTuhmAEBSzOzxKq9jyggAIImAAAAYISAAACQREAAAIwQEAIAkAgKAVZVvssVN\nt5JFQACwvK0t6cSJvSDgnm1vbYVsVb16FPAICACW4y5dvChtb+8FhRMnsu2LF7tx4uxDwCtI6sI0\nABExk+66K/t5ezt7SNJwmD1vFq5tdSgGPCl7T3nAGw6zv0/9PZaYJxTF19fXnSuVgci4S1cUJht2\nd7tzoiyOenIJBjwzO+/u6/Nex5QRgOXlJ8yi4hRL6oqjoFxiwWARBAQAyyn2nofDbGQwHI7nFFLX\n9YBXQkA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RAgBAEjkEAMAIAQEAIImAAAAYISAAACQREAAAIwQEAIAk\nAgIAYISAAACQJP0/jxD1E8UGy9gAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x5ea3748>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "x = load_data(\"kmeans/testSet.txt\")\n",
    "print x.shape\n",
    "\n",
    "#可视化一下\n",
    "plt.scatter(x[:,0],x[:,1],marker='x',color = 'r')\n",
    "plt.xlabel('x0')\n",
    "plt.ylabel('x1')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算距离\n",
    "def computeDistance(A,B):\n",
    "    return np.sqrt(np.sum(np.square(A-B)))\n",
    "\n",
    "#我们一开始应该随机初始化centroids ,随机选择k个样本点作为质心,而不是书上的某个随机值\n",
    "def randCentroids(x,k=3):\n",
    "    m,n = x.shape\n",
    "    centroids = np.zeros((k,n))\n",
    "    randIndex = np.random.choice(m,k)\n",
    "    centroids = x[randIndex]\n",
    "    return centroids\n",
    "\n",
    "#计算x的均值，改变centroids\n",
    "def change_centroids(x,idx,K):\n",
    "    m,n = x.shape\n",
    "    centroids = np.zeros((K,n))\n",
    "    for i in range(K):\n",
    "        index = np.where(idx[:,0].ravel() == i)\n",
    "        centroids[i] = np.mean(x[index],axis=0)\n",
    "    return centroids\n",
    "\n",
    "#为数据集x找到最近的质心,直到质心不变为止   \n",
    "def kmeans(x,K):\n",
    "    #1.初始化质心\n",
    "    centroids = randCentroids(x,K)\n",
    "    m = x.shape[0]\n",
    "    idx = np.zeros((x.shape[0],2)) #记录x对应的质心的下标 + 平方误差\n",
    "    clusterchanged = True #记录分类变没变化\n",
    "    cent = {} #用字典，来存储各个质心的变化\n",
    "    for k in xrange(K):\n",
    "        cent[\"%d\"%(k)] = []\n",
    "        cent[\"%d\"%(k)].append(centroids[k,:].tolist())\n",
    "    while clusterchanged:\n",
    "        \n",
    "        clusterchanged = False\n",
    "        #2.为每个样本点分配质心\n",
    "        for i in range(m):\n",
    "            minDist = np.inf\n",
    "            minIndex = -1\n",
    "            for j in range(K):\n",
    "                distance = computeDistance(x[i,:],centroids[j,:])\n",
    "                if distance<minDist:\n",
    "                    minDist = distance\n",
    "                    minIndex = j\n",
    "            if idx[i,0] != minIndex:\n",
    "                clusterchanged = True\n",
    "            idx[i,:] = minIndex, minDist**2\n",
    "        #3.更新质心的位置\n",
    "        centroids = change_centroids(x,idx,K)\n",
    "        for k in xrange(K):\n",
    "            cent[\"%d\"%(k)].append(centroids[k,:].tolist())\n",
    "    return centroids,idx,cent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.6265299   3.10868015]\n [-2.46154315  2.78737555]\n [ 2.65077367 -2.79019029]\n [-3.53973889 -2.89384326]]\n[[ 0.          2.3201915 ]\n [ 1.          1.39004893]\n [ 2.          7.46974076]\n [ 3.          3.60477283]\n [ 0.          2.7696782 ]]\n[[ 2.336445    2.875106  ]\n [ 2.53499033  2.93781533]]\n"
     ]
    }
   ],
   "source": [
    "centroids,idx,cent = kmeans(x,K = 4) \n",
    "print centroids\n",
    "print idx[:5]\n",
    "print np.array(cent[\"0\"])[:2] #转化成array()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEKCAYAAAASByJ7AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X94XGWZN/Dv3ZSVS36FQsHXtmkCVVcWIdII4XUVlNqG\nLsIugivr9qJ2taJApkFkFdZkWn6IsraEll2WldaucKHbRRC1hF8WfK/XtpqwKQuUlkrSlK6uIERS\nfMFmzv3+ceYkZ07OTM5kzpnn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dwhERBSNJN4hEBFRBBgQiIgIAAMCERHlMSAQEREABgQiIspjQCAqQ35VzwER\nmZE/Pzp/3igiPSIyXEurX1K6MCAQlUFV9wH4ZwA35x+6GcCdqjoI4BbYY/GJEokBgah8awC0isgK\nAH+O/BLsqvo4gBGTBSOqRCzXMiKKM1U9KCJfBtADYKGqHjRdJqIw8A6BaGrOBfBrACebLghRWBgQ\niMokIs0APgZ7CfYOEflfhotEFAoGBKIy5Ff1/GfY6/0PwU4k1+w2rpQuDAhE5fkcgCFVfTR//k8A\n3isiZ4nI/wGwCcA5IvKSiCwyVkqiKeBqp0REBIB3CERElMeAQEREABgQiIgojwGBiIgAMCAQEVEe\nAwIREQFgQCAiojwGBCIiAgD8f0XOik0Dzv2EAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f29048>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#可视化一下质心的变化和最后质心的位置  、 以及分类的结果\n",
    "#你会发现，每次运行上面的结果，都会产生一点差异，这是因为\n",
    "#1.随机选取质心  2.K的不同 都会影响kmeans聚类的结果\n",
    "#besides:k应该选取多少？  \n",
    "#缺点，容易收敛到局部最少值，在大数据集收敛较慢\n",
    "idx0 = np.where(idx[:,0]==0)\n",
    "idx1 = np.where(idx[:,0]==1)\n",
    "idx2 = np.where(idx[:,0]==2)\n",
    "idx3 = np.where(idx[:,0]==3)\n",
    "plt.scatter(x[idx0,0],x[idx0,1],marker='x',color = 'r')\n",
    "plt.scatter(x[idx1,0],x[idx1,1],marker='x',color = 'g')\n",
    "plt.scatter(x[idx2,0],x[idx2,1],marker='x',color = 'y')\n",
    "plt.scatter(x[idx3,0],x[idx3,1],marker='x',color = 'b')\n",
    "plt.scatter(centroids[:,0],centroids[:,1],marker='o',color = 'black',linewidths=10)\n",
    "K = 4\n",
    "for k in xrange(K):\n",
    "    plt.plot(np.array(cent[\"%d\"%(k)])[:,0],np.array(cent[\"%d\"%(k)])[:,1],\"b-o\")\n",
    "plt.xlabel('X1')\n",
    "plt.ylabel('X2')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(60L, 2L)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEWCAYAAABmE+CbAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHHxJREFUeJzt3X2wXVd53/HvT7LBThykxFLjYluIUpLGdUCpZJyJS+OC\nh5qg2tNppuHNKSEdN1MYXb9QB2xiXZNm0g7TK66SdIiaNEkxCUNqCK5LgqGNaV5qsOTYgDEQY2xs\nYieyIwknkMTyffrH3jt3n+t7Xu552WvtfX6fmTPSeblnr/O2n73Xs561FBGYmZltSt0AMzPLgwOC\nmZkBDghmZlZyQDAzM8ABwczMSg4IZmYGOCBYApJ2SgpJp6Ruy6xJekjSJSM+9k2Sfn/WbTLrxwHB\nZm4jO0UbjaRFSTd3ZTuWBwcEMzMDHBBsxiS9D9gB/E9JfyHputrdb5D0VUlPSLqh9jebJL1d0pcl\nPSnpg5K+o8/zb5N0m6Tjkv5c0u9J2lTe93xJt0g6KukrkvbV/m6zpOvLbTwl6Yikc8v7fkDSXZJO\nlP/+QO3v7pD005L+oPy72yVtq91/haSHy3bfwACSzpR0q6SvS/o08KI19y9LeqS8/4ikl5e3Xwpc\nD/xI+Z7eW97+Y5LuL9v1oKR/O8n71G871mER4YsvM70ADwGX1K7vBAL4r8DpwEuBvwa+p7x/AbgT\nOAd4LvCLwG/0ee6fBd4LnFpeXg6I4mDnCHAj8Bzg7wEPAv+s/Lt/D3wW+O7y8S8FzgS+AzgGXAGc\nAryuvH5m+Xd3AF8Gvqts+x3AfyzvOw/4C+CflO1eAk7WX/uatn8A+CDwrcD5wNeA36/d/8ayTacA\n1wKPA6eV9y0CN695vtdQBBUBPwh8A/hHE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wF3hl\nzEfBy9eAc2vXzylvmyuSTqUIBu+PiA+lbk/DLgIuk/RDwGnA8yTdHBFvTNyukbgwrQGSHgL2REQu\nMx/OnKRLgSXgByPiaOr2NEHSKRQJ9FdSBIK7gNdHxH1JG9YgFUdAvwb8eURclbo9KZVnCG+LiL2p\n2zIq5xBsVn4e+Dbg45LukfTe1A2atTKJ/lbgYxTJ1A/OUzAoXQRcAbyi/NzvKY+WrQV8hmBmZoDP\nEMzMrOSAYGZmgAOCmZmVHBDMzAxwQDAzs5IDgtmUSPodScfbNsOlWcUBwWx63k0xBt+slRwQzDZI\n0gXlOg+nSfrWct7/8yPifwNPpW6f2bg8l5HZBkXEXZJuBf4DcDpwc0R8LnGzzCbmgGA2nndRzFX0\nV8C+xG0xmwp3GZmN50zgDIr5mk5L3BazqXBAMBvPLwI/RbHOw39K3BazqXCXkdkGSfpR4OmI+PVy\nHeU/lPQK4CbgHwBnSHoU+PGI+FjKtppthGc7NTMzwF1GZmZWckAwMzPAAcHMzEoOCGZmBjggmJlZ\nyQHBzMwABwQzMyv9f+EvTMH8/5WXAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7ca56d8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#二分 kmeans分类\n",
    "#首先将所有点视为一个簇，然后每次一分为二，然后再将其中一个簇继续进行划分，\n",
    "#选择哪个簇进行划分，取决于对其划分是否最大程度降低SSE的值（sum of square error 误差平方和）\n",
    "\n",
    "\n",
    "xx = load_data(\"kmeans/testSet2.txt\")\n",
    "print xx.shape\n",
    "\n",
    "plt.scatter(xx[:,0],xx[:,1],marker=\"x\",color = 'red')\n",
    "plt.xlabel(\"x1\")\n",
    "plt.ylabel(\"x2\")\n",
    "plt.title(\"the second dataset\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def biKmeans(x,k):\n",
    "    m,n = x.shape\n",
    "    idx = np.zeros((m,2)) #x对应的下标+平方误差\n",
    "    centroid0 = np.mean(x,axis=0).tolist() #转化成list\n",
    "    centList = [centroid0] #初始化一个质心\n",
    "    for j in range(m): #初始的平方误差\n",
    "        idx[j,1] = computeDistance(np.array(centroid0),x[j,:]) ** 2\n",
    "    \n",
    "    while (len(centList) < k): #直到划分k类\n",
    "        lowestSSE = np.inf #无穷大\n",
    "        bestCentToSplit = None\n",
    "        bestNewCents = None\n",
    "        bestIndex = None\n",
    "        for i in range(len(centList)): \n",
    "            #遍历每一个簇,将该簇中的所有点视为一个小的数据集，进行k=2的kmeans（）划分\n",
    "            data = x[idx[:,0] == i,:]\n",
    "            cent,index,cent_dic = kmeans(data,2)\n",
    "            SSE_split = np.sum(index[:,1]) #划分部分的SSE和\n",
    "            SSE_noSplit = np.sum(idx[idx[:,0]!=i,1]) #没有划分部分的SSE和\n",
    "            \n",
    "            if (SSE_noSplit + SSE_split) < lowestSSE: #选择最优的划分方式\n",
    "                bestCentToSplit = i\n",
    "                bestNewCents = cent\n",
    "                bestIndex = index.copy()  #记录最好的划分情况 这里的分类只有0/1\n",
    "\n",
    "                lowestSSE = SSE_noSplit + SSE_split\n",
    "                \n",
    "        bestIndex[bestIndex[:,0]==1,0] = len(centList) #将1-->len(),即增加一个分类\n",
    "        bestIndex[bestIndex[:,0]==0,0] = bestCentToSplit #将0-->划分的那个i\n",
    "        centList[bestCentToSplit] = bestNewCents[0,:].tolist() #被划分的第i簇，质心改一下\n",
    "        centList.append(bestNewCents[1,:].tolist()) #增加一个质心\n",
    "        idx[idx[:,0] == bestCentToSplit,:] = bestIndex #被划分的第i簇，对应的下标，改成bestIndex\n",
    "        #有原来的1个变成了两个    \n",
    "    return np.array(centList),idx\n",
    "\n",
    "\n",
    "        \n",
    "    \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3L, 2L)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+\nImlv2EoS920VfgkcC11Irbim8jSZ2Y8lXVbhWw9K+poK00UNa6rP7+5/W3zMgypMJTyaZG0Ix8za\nJP21pDXu/k7oepJiZrdIetPd95nZjaHrqRWBME3u/olKXzezayR1S9pvZlJhuuQ5M7vO3V9PsMRY\nTfb5S8xspaRbJN3kzXGO888ldZXdn1f8WtMws/eoEAaPuvvfhK4nYTdIutXMVkiaJWmOmf25u38+\ncF1VYR1CQsxsWFKPu6dls6vYmVmvpG9J+pi7HwldTxLMbKYKDfSbVAiCZyXd7u4HghaWECv89vOI\npBF3XxO6npCKI4TfcfdbQtdSLXoIiNNGSZdIesrMhszsT0IXFLdiE/1uST9SoaH6eLOEQdENkr4g\n6ePF/+ZDxd+WkQGMEAAAkhghAACKCAQAgCQCAQBQRCAAACQRCACAIgIBqEFxN8+XzayjeP/9xfs5\nM9tuZkeztsMlUEIgADVw91clfVfSHxa/9IeSNrv7sKRvqnAOPpBJBAJQu/WSlpnZGkn/UtIfSZK7\n/09Jx0MWBkwHexkBNXL3M2Z2n6Ttkm529zOhawKiwAgBqM+nJb0m6UOhCwGiQiAANTKzJZI+qcIV\nwdaa2eWBSwIiQSAANSju5vldFfb5/6kKjeQ/ClsVEA0CAajNlyT91N2fKt7/j5KuNrOPmdn/krRN\n0k1m9jMz+1SwKoE6sNspAEASIwQAQBGBAACQRCAAAIoIBACAJAIBAFBEIAAAJBEIAIAiAgEAIEn6\n/8vfbfCz5mmpAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x5878a90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cent,index = biKmeans(xx,k=3)\n",
    "print cent.shape\n",
    "print index.shape\n",
    "\n",
    "idx0 = np.where(index[:,0]==0)\n",
    "idx1 = np.where(index[:,0]==1)\n",
    "idx2 = np.where(index[:,0]==2)\n",
    "\n",
    "plt.scatter(xx[idx0,0],xx[idx0,1],marker='x',color = 'r')\n",
    "plt.scatter(xx[idx1,0],xx[idx1,1],marker='+',color = 'b')\n",
    "plt.scatter(xx[idx2,0],xx[idx2,1],marker='*',color = 'y')\n",
    "\n",
    "plt.scatter(cent[:,0],cent[:,1],marker='o',color = 'black',linewidths=10)\n",
    "\n",
    "plt.xlabel('X1')\n",
    "plt.ylabel('X2')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
